//
//  GuideMovieViewController.m
//  OurJourney
//
//  Created by 李沛池 on 2017/4/12.
//  Copyright © 2017年 Godlike. All rights reserved.
//

#import "GuideMovieViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import "LPCDefine.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface GuideMovieViewController ()

@property (strong, nonatomic) MPMoviePlayerController *player;

@end

@implementation GuideMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)dealloc
{
    
}

- (void)initUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self SetupVideoPlayer];
}

- (void)SetupVideoPlayer
{
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:_movieURL];
    [self.view addSubview:self.player.view];
    self.player.shouldAutoplay = YES;
    [self.player setControlStyle:MPMovieControlStyleNone];
    self.player.repeatMode = MPMovieRepeatModeOne;
    [self.player.view setFrame:self.view.bounds];
    self.player.view.alpha = 0;
    [UIView animateWithDuration:3 animations:^{
        self.player.view.alpha = 1;
        [self.player prepareToPlay];
    }];
    
    [self setupLoginView];
}

- (void)setupLoginView
{
    //进入按钮
    UIButton *enterMainButton = [[UIButton alloc] init];
    enterMainButton.frame = CGRectMake(24, [UIScreen mainScreen].bounds.size.height - 32 - 48, [UIScreen mainScreen].bounds.size.width - 48, 48);
    enterMainButton.layer.borderWidth = 1;
    enterMainButton.layer.cornerRadius = 24;
    enterMainButton.layer.borderColor = [UIColor cyanColor].CGColor;
    [enterMainButton setTitle:@"翻开纪念册" forState:UIControlStateNormal];
    [enterMainButton setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    enterMainButton.alpha = 0;
    [self.player.view addSubview:enterMainButton];
    [enterMainButton addTarget:self action:@selector(enterMainAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [UIView animateWithDuration:3.0 animations:^{
        enterMainButton.alpha = 1.0;
    }];
}

- (void)enterMainAction:(UIButton *)btn
{
//    [self touchID];
    
    [self.player stop];
    
    UITabBarController *tabBC = XIBFromStoryBoard(@"OurJourneyTabBarController");
    [UIApplication sharedApplication].delegate.window.rootViewController = tabBC;
//    [self presentViewController:tabBC animated:NO completion:nil];
}

//指纹校验
- (void)touchID
{
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    
    // Hide "Enter Password" button
    myContext.localizedFallbackTitle = @"";
    
    NSString *myLocalizedReasonString = [NSString stringWithFormat:@"通过验证指纹解锁%@", @"Godlike"];
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])
    {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
                                if (success) {
                                    // User authenticated successfully, take appropriate action
                                    
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        /**
                                         指纹校验 成功
                                         */
                                    });
                                    
                                }else {
                                    // User did not authenticate successfully, look at error and take appropriate action
                                    
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        /**
                                         指纹校验 失败
                                         */
                                    });
                                }
                            }];
    }
}


@end
